package com.dd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.dto.DishDTO;
import com.dd.entity.Dish;
import com.dd.exception.BusinessException;
import com.dd.mapper.DishMapper;
import com.dd.service.IDishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 菜品表 服务实现类
 * </p>
 *
 * @author dhd
 * @since 2025-01-12 09:32
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {

    @Autowired
    private DishMapper dishMapper;

    @Override
    @Transactional
    public Dish create(DishDTO dishDTO) {
        // 检查菜品名是否已存在
        if (isNameExists(dishDTO.getName())) {
            throw new BusinessException("菜品名称已存在");
        }

        Dish dish = new Dish();
        dish.setName(dishDTO.getName());
        dish.setCategoryId(dishDTO.getCategoryId());
        dish.setPrice(dishDTO.getPrice());
        dish.setDescription(dishDTO.getDescription());
        dish.setImageUrl(dishDTO.getImageUrl());
        dish.setStatus(dishDTO.getStatus());

        dishMapper.insert(dish);
        return dish;
    }

    @Override
    @Transactional
    public Dish update(Long id, DishDTO dishDTO) {
        Dish dish = dishMapper.selectById(id);
        if (dish == null) {
            throw new BusinessException("菜品不存在");
        }

        // 如果名称变更，检查新名称是否已存在
        if (!dish.getName().equals(dishDTO.getName()) && isNameExists(dishDTO.getName())) {
            throw new BusinessException("菜品名称已存在");
        }

        dish.setName(dishDTO.getName());
        dish.setCategoryId(dishDTO.getCategoryId());
        dish.setPrice(dishDTO.getPrice());
        dish.setDescription(dishDTO.getDescription());
        dish.setImageUrl(dishDTO.getImageUrl());
        dish.setStatus(dishDTO.getStatus());

        dishMapper.updateById(dish);
        return dish;
    }

    @Override
    @Transactional
    public void delete(Long id) {
        Dish dish = dishMapper.selectById(id);
        if (dish == null) {
            throw new BusinessException("菜品不存在");
        }

        dishMapper.deleteById(id);
    }

    @Override
    public Dish getById(Long id) {
        Dish dish = dishMapper.selectById(id);
        if (dish == null) {
            throw new BusinessException("菜品不存在");
        }
        return dish;
    }

    @Override
    public Page<Dish> list(int page, int size, String name, Long categoryId) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<Dish>()
                .like(StringUtils.hasText(name), Dish::getName, name)
                .eq(categoryId != null, Dish::getCategoryId, categoryId)
                .orderByDesc(Dish::getCreatedAt);

        return dishMapper.selectPage(new Page<>(page, size), queryWrapper);
    }

    @Override
    @Transactional
    public void updateStatus(Long id, Byte status) {
        Dish dish = dishMapper.selectById(id);
        if (dish == null) {
            throw new BusinessException("菜品不存在");
        }

        dish.setStatus(status);
        dishMapper.updateById(dish);
    }

    private boolean isNameExists(String name) {
        return dishMapper.selectCount(
                new LambdaQueryWrapper<Dish>()
                        .eq(Dish::getName, name)
        ) > 0;
    }
}
